<?php

class User extends CActiveRecord {
    /**
     * The followings are the available columns in table 'tbl_user':
     * @var integer $id
     * @var string $username
     * @var string $password
     * @var string $email
     */

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, email, password2','required','on'=>'reg'),
            array('password2','compare','compareAttribute'=> 'password','on'=>'reg'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, username, password, email', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'user_id' => 'user_id',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
        );
    }
    
    public function recently ($limit,$sdvig)	
    {
            $this->getDbCriteria()->mergeWith(array(
                'order'=>'user_id DESC',
                'limit'=>$limit,
                'offset'=>$sdvig


            ));
        return $this;

     }

    public function relations() {
        return array(
            'user_info' => array(self::BELONGS_TO, 'user_info', 'user_id'),
            'user_subjekt' => array(self::HAS_MANY, 'user_subjekt', 'user_id'),
            'user_tag' => array(self::HAS_MANY, 'user_tag', 'user_id'),
            'user_friend'=>array(self::HAS_MANY,'user_friend','id_user_friend'),
            'order'=>array(self::HAS_MANY,'order','user_id')
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);

        $criteria->compare('username', $this->username, true);

        $criteria->compare('password', $this->password, true);

        $criteria->compare('email', $this->email, true);

        return new CActiveDataProvider('User', array(
            'criteria' => $criteria,
        ));
    }

}